package com.xsky.logic.groupmessage;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.DateConverter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.xsky.converter.XStreamClobConverter;
import com.xsky.database.dao.impl.BaseEntityDaoImpl;
import com.xsky.operation.vo.MessageGroupUser;
import com.xsky.operation.vo.TabClient;
import com.xsky.operation.vo.TabEngineer;
import com.xsky.operation.vo.TabGroupMessage;
import com.xsky.operation.vo.TabUserRole;

public class GroupMessageLogic
{
	public String add(String xmlStr)//增加
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		XStream xs = new XStream(new DomDriver());
		String defaultFormat = "yyyy-MM-dd";
		String[] acceptableFormat = {"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd","yyyy-MM"};
		xs.registerConverter(new DateConverter(defaultFormat,acceptableFormat));
		xs.registerConverter(new XStreamClobConverter());
		xs.alias("data", TabGroupMessage.class);
		TabGroupMessage entity = (TabGroupMessage)xs.fromXML(xmlStr);
		entity.setId(UUID.randomUUID().toString().substring(0,31));
		baseDao.insertEntity(entity);
		return "<data><result>ok</result><uuid>"+entity.getId()+"</uuid><info>添加成功</info></data>";
	}
	public String delete(String xmlStr)//删除
	{
		String dataXml="";
		String in = "(";
		try
		{
			Document document = DocumentHelper.parseText(xmlStr);
			List itemList = document.selectNodes("//root/item");
			Element element = null;
			for(int i = 0; i < itemList.size(); i++)
			{
				element = (Element)itemList.get(i);
				document = DocumentHelper.parseText(element.asXML());
				if(element.attribute("selected")==null)
				{
					
				}
				else
				{
					if("true".equals(element.attribute("selected").getData().toString()))
					{
						in+="'"+document.selectSingleNode("//item/id").getText()+"',";
					}
				}
			}
		} catch (DocumentException e)
		{
			e.printStackTrace();
		}
		in += ")";
		in = in.replace(",)", ")");
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		baseDao.deleteByHql("delete TabGroupMessage model where model.id in "+in);
		return "<data><result>ok</result><uuid></uuid><info>删除成功</info></data>";
	}
	public String update(String xmlStr)//更新报送信息
	{
		String dataXml="";
		try
		{
			Document document = DocumentHelper.parseText(xmlStr);
			dataXml = document.selectSingleNode("//data").asXML();
		} catch (DocumentException e)
		{
			e.printStackTrace();
		}
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		XStream xs = new XStream(new DomDriver());
		String defaultFormat = "yyyy-MM-dd HH:mm:ss";
		String[] acceptableFormat = {"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd","yyyy-MM"};
		xs.registerConverter(new DateConverter(defaultFormat,acceptableFormat));
		xs.registerConverter(new XStreamClobConverter());
		xs.alias("data", TabGroupMessage.class);
		TabGroupMessage entity = (TabGroupMessage)xs.fromXML(dataXml);
		baseDao.updateEntity(entity);
		return "<data><result>ok</result><uuid></uuid><info>更新成功</info></data>";
	}
	public String genDetail(String id)
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		TabGroupMessage entity = new TabGroupMessage();
		entity = (TabGroupMessage)baseDao.findByPrimaryKey(entity, "id", id);
		XStream xs = new XStream(new DomDriver());
		xs.alias("data", TabGroupMessage.class);
		String xmlStr = xs.toXML(entity);
		return xmlStr;
	}
	public String genAll()
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		List list = baseDao.findByHql("from TabGroupMessage model");
		String result = "<data>";
		XStream xs = new XStream(new DomDriver());
		String defaultFormat = "yyyy-MM-dd";
		String[] acceptableFormat = {"yyyy-MM"};
		xs.registerConverter(new DateConverter(defaultFormat,acceptableFormat));
		xs.registerConverter(new XStreamClobConverter());
		xs.alias("item", TabGroupMessage.class);
		for (int i =0; i<list.size(); i++)
		{
			TabGroupMessage entity = (TabGroupMessage)list.get(i);
			result+=xs.toXML(entity);
		}
		result+="</data>";
		return result.replace("00:00:00.0", "");
	}
	public String genWithCondition(String condition)
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		String result = "<data>";
		try
		{
			Document document = DocumentHelper.parseText(condition);
			String parentId = document.selectSingleNode("//condition/parentId").getText();
			String code = document.selectSingleNode("//condition/code").getText();
			String value = document.selectSingleNode("//condition/value").getText();
			String hql = "";
			hql+="from TabGroupMessage model where model.parentId='"+parentId+"'";
			hql+=" and model.code like '%"+code+"%'";
			hql+=" and model.value like '%"+value+"%'";
			List list = baseDao.findByHql(hql);
			XStream xs = new XStream(new DomDriver());
			String defaultFormat = "yyyy-MM";
			String[] acceptableFormat = {"yyyy-MM"};
			xs.registerConverter(new DateConverter(defaultFormat,acceptableFormat));
			xs.registerConverter(new XStreamClobConverter());
			xs.alias("item", TabGroupMessage.class);
			for (int i =0; i<list.size(); i++)
			{
				TabGroupMessage entity = (TabGroupMessage)list.get(i);
				result+=xs.toXML(entity);
			}
		} catch (DocumentException e)
		{
			e.printStackTrace();
		}
		result+="</data>";
		return result.replace("-01 00:00:00.0", "");
	}
	public String genByType(String type)
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		String result = "<data>";
		String hql = "";
		hql+="from TabGroupMessage model where model.name like '%"+type+"%'";
		List list = baseDao.findByHql(hql);
		XStream xs = new XStream(new DomDriver());
		String defaultFormat = "yyyy-MM";
		String[] acceptableFormat = {"yyyy-MM"};
		xs.registerConverter(new DateConverter(defaultFormat,acceptableFormat));
		xs.registerConverter(new XStreamClobConverter());
		xs.alias("item", TabGroupMessage.class);
		for (int i =0; i<list.size(); i++)
		{
			TabGroupMessage entity = (TabGroupMessage)list.get(i);
			result+=xs.toXML(entity);
		}
		result+="</data>";
		return result.replace("-01 00:00:00.0", "");
	}
	public String genUsers(String groupId)
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		List userRoleList = baseDao.findByHql("from MessageGroupUser model where model.groupId='"+groupId+"'");
		String result = "<data>";
		for(int i = 0; i < userRoleList.size(); i++)
		{
			MessageGroupUser entity = (MessageGroupUser)userRoleList.get(i);
			String userId = entity.getUserId();
			String id = entity.getId();
			TabEngineer engineer = new TabEngineer();
			engineer = (TabEngineer)baseDao.findByPrimaryKey(engineer, "id", userId);
			result+="<item>";
			result+="<code>"+engineer.getJn()+"</code>";
			result+="<id>"+id+"</id>";
			result+="<realName>"+engineer.getRealname()+"</realName>";
			result+="<userName>"+engineer.getUsername()+"</userName>";
			result+="<lxfs>"+engineer.getPhone()+"/"+engineer.getMobile()+"/"+engineer.getEmail()+"</lxfs>";
			result+="<userId>"+engineer.getId()+"</userId>";
			result+="<groupId>"+groupId+"</groupId>";
			result+="</item>";
		}
		result+="</data>";
		return result;
	}
	public String genUsersNotInGroup(String groupId)
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		List userRoleList = baseDao.findByHql("from MessageGroupUser model where model.groupId='"+groupId+"'");
		String result = "<data>";
		String ids="(";
		for(int i = 0; i < userRoleList.size(); i++)
		{
			
			MessageGroupUser entity = (MessageGroupUser)userRoleList.get(i);
			String userId = entity.getUserId();
			ids+="'"+userId+"',";
		}
		ids+=")";
		ids=ids.replace("',)", "')");
		List engineerList = new ArrayList();
		if(!"()".equals(ids))
		{
			engineerList = baseDao.findByHql("from TabEngineer model where model.id not in "+ids);
		}
		else
		{
			engineerList = baseDao.findByHql("from TabEngineer model");
		}
		for(int i = 0 ; i < engineerList.size(); i++)
		{
			TabEngineer engineer = (TabEngineer)engineerList.get(i);
			result+="<item>";
			result+="<code>"+engineer.getJn()+"</code>";
			result+="<realName>"+engineer.getRealname()+"</realName>";
			result+="<userName>"+engineer.getUsername()+"</userName>";
			result+="<lxfs>"+engineer.getPhone()+"/"+engineer.getMobile()+"/"+engineer.getEmail()+"</lxfs>";
			result+="<id>"+engineer.getId()+"</id>";
			result+="</item>";
		}
		result+="</data>";
		return result;
	}
	public String addUserGroup(String groupId,String users)
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		try
		{
			Document document = DocumentHelper.parseText(users);
			List nodes = document.selectNodes("//data/item");
			for(int i = 0; i < nodes.size(); i++)
			{
				Element element = (Element)nodes.get(i);
				if(element.attribute("selected")!=null&&"true".equals(element.attribute("selected").getText()))
				{
					String userId = element.selectSingleNode("id").getText();
					MessageGroupUser messageGroupUser = new MessageGroupUser();
					messageGroupUser.setId(UUID.randomUUID().toString().substring(0,31));
					messageGroupUser.setGroupId(groupId);
					messageGroupUser.setUserId(userId);
					baseDao.insertEntity(messageGroupUser);
				}
			}
		} catch (DocumentException e)
		{
			e.printStackTrace();
		}
		return "";
	}
	public String deleteMessageGroupUser(String xmlStr)
	{
		String in = "(";
		try
		{
			Document document = DocumentHelper.parseText(xmlStr);
			List itemList = document.selectNodes("//root/item");
			Element element = null;
			for(int i = 0; i < itemList.size(); i++)
			{
				element = (Element)itemList.get(i);
				document = DocumentHelper.parseText(element.asXML());
				if(element.attribute("selected")==null)
				{
					
				}
				else
				{
					if("true".equals(element.attribute("selected").getData().toString()))
					{
						in+="'"+document.selectSingleNode("//item/id").getText()+"',";
					}
				}
			}
		} catch (DocumentException e)
		{
			e.printStackTrace();
		}
		in += ")";
		in = in.replace(",)", ")");
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		baseDao.execute("delete MessageGroupUser model where model.id in "+in);
		return "";
	}
	public String deleteMessageGroupUserById(String groupId,String userId)
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		baseDao.execute("delete MessageGroupUser model where model.groupId='"+groupId+"' and model.userId='"+userId+"'");
		return "";
	}
}
